﻿package com.buaadu.clock;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import java.util.*;

public class Clock extends JPanel{
	int h, m, s;// 小时，分钟，秒
	double rad = Math.PI / 180;
	
	private NeedleIconManger needleIconManger;
	
	int x,y,r;
	
	private Date date;

	public Clock(Date date) {
		
		this.date = date;
		
		needleIconManger = new NeedleIconManger();

		
		this.addComponentListener(new   ComponentAdapter()   {  
            public   void   componentResized(ComponentEvent   e)   {  
            	refreshSize();
            	refreshDate();
            	repaint();
            }  
        });
		refreshSize();
		refreshDate();
	}
	public void setDate(Date date){
		this.date = date;
		refreshDate();
		repaint();
	}
	private void refreshDate(){
		Calendar calendar = new GregorianCalendar();
		if(date != null){
			calendar.setTime(date);
		}
		s = calendar.get(Calendar.SECOND) * 6;// 获得秒转换成度数
		m = calendar.get(Calendar.MINUTE) * 6;// 获得分钟
		h = (calendar.get(Calendar.HOUR_OF_DAY) - 12) * 30
				+ calendar.get(Calendar.MINUTE) / 12 * 6;// 获得小时
	}
	private void refreshSize(){
		int minLen = Math.min(getWidth(), getHeight());
		
		int offset = (int)(minLen * 0.05);
		
		r = (minLen - offset*2)/2;
		
		if(getWidth() > getHeight()){
			x = (getWidth() - getHeight())/2 + offset;
			y = offset;
		}else{
			x = offset;
			y = (getHeight() - getWidth())/2 + offset;
		}
		needleIconManger.setRadius(r);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		//x = 20 y = 20 r = 70
		// 清屏
		
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);   
//		g2.setColor(Color.BLACK);
//		g2.fillRect(0, 0, r * 3, r * 3);
		// 画圆
		//g2.setColor(Color.BLACK);
		//g2.fillRect(x, y, r * 2, r * 2);
		g2.setColor(new Color(0,119,119));
		g2.drawOval(x, y, r * 2, r * 2);
		// 秒针
//		g2.setColor(Color.RED);
		int x1 = (int) ((r - 10) * Math.sin(rad * s));
		int y1 = (int) ((r - 10) * Math.cos(rad * s));
//		g2.drawLine(x + r, y + r, x + r + x1, y + r - y1);
		
		Graphics2D g2Copy3 = (Graphics2D)g2.create();
		
		g2Copy3.translate(x + r, y + r);
		g2Copy3.rotate(rad * (h - 90));
		needleIconManger.getHourNeedleIcon().paintIcon(this, g2Copy3, 0, 0);
		
		Graphics2D g2Copy2 = (Graphics2D)g2.create();
		
		g2Copy2.translate(x + r, y + r);
		g2Copy2.rotate(rad * (m - 90));
		needleIconManger.getMinuteNeedleIcon().paintIcon(this, g2Copy2, 0, 0);
		
		
		Graphics2D g2Copy = (Graphics2D)g2.create();
		g2Copy.setColor(Color.RED);
		
		g2Copy.translate(x + r, y + r);
		g2Copy.rotate(rad * (s - 90));
		needleIconManger.getSecondNeedleIcon().paintIcon(this, g2Copy, 0, 0);
		
		
		// 数字
		g2.setColor(new Color(0,119,119));
		
		g2.fillOval(x+r-3, y+r-3, 6, 6);
		int d = 29;
		for (int i = 1; i <= 12; i++) {
			x1 = (int) ((r - 10) * Math.sin(rad * d));
			y1 = (int) ((r - 10) * Math.cos(rad * d));
			g2.drawString(i + "", x + r + x1 - 4, y + r - y1 + 5);
			d += 30;
		}
		// 小点
		d = 0;
		for (int i = 0; i < 60; i++) {
			x1 = (int) ((r - 2) * Math.sin(rad * d));
			y1 = (int) ((r - 2) * Math.cos(rad * d));
			g2.drawString(".", x + r + x1 - 1, y + r - y1 + 1);
			d += 6;
		}

	}
}